package com.fhz.a2102high6

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 时间:2023/9/9
 * @author Mr.Feng
 * 简述: kotlin的联系
 */
/**
 * 顶层就是相当于java中的静态方法
 */
val PI = 3.14

fun main(){

    var useer = Useer("小明",16)
    val age1 = useer.age
    useer.age  = 18
    val age2 = useer.age //18


    CoroutineScope(Dispatchers.IO).launch{

    }





    println("kotlin")
    var age = 0
    age = 1


    println("年龄 = $age ")


    val add = add(1, 2)
    println(add)

    var list  =  ArrayList<String>()
    list.add("12")

//    val listOf = listOf("apple", "banana", "kiwifruit")
    val listOf = mutableListOf("apple", "banana", "kiwifruit")
    listOf.add("12")


    val describe = describe(1)



    val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
    fruits.filter { it.startsWith("a") }
        .sortedBy { it }
        .map { it.toUpperCase() }
        .forEach { println(it) }
    val oneMillion = 1_0_0_0_0_0_0

    val str = "hello"
    for (c in str) {
        println(c)
    }


}

fun describe(obj: Any): String =
    when (obj) {
        1          -> "One"
        "Hello"    -> "Greeting"
        is Long    -> "Long"
        !is String -> "Not a string"
        else       -> "Unknown"
    }
fun add(a:Int,b:Int)  = a + b

fun maxOf(a: Int, b: Int) = if (a > b) a else b



fun parse(a:Any){
    if(a is Int){

    }else if(a is String){

    }
}



fun parseInt(str: String): Int? {
    if(str != ""){
       return 1
    }else{
       return null
    }
}

fun printProduct(arg1: String, arg2: String) {
    val x = parseInt(arg1)
    val y = parseInt(arg2)


    // 直接使用 `x * y` 会导致编译错误，因为它们可能为 null
    if (x != null && y != null) {
        // 在空检测后，x 与 y 会自动转换为非空值（non-nullable）
        println(x * y)
    }
    else {
        println("'$arg1' or '$arg2' is not a number")
    }
}